from django.db import models
from User.models import *
from goods.models import *
# Create your models here.
#订单模型
class Order(models.Model):
    out_trade_num = models.UUIDField(auto_created=True,unique=True) #调用支付时使用的商品订单号
    order_num = models.CharField(max_length=50)                     #20200608093222   给用户看的订单号
    address = models.ForeignKey(Address,on_delete=models.CASCADE)   #收货地址
    user = models.ForeignKey(User,on_delete=models.CASCADE)         #用户
    trade_no = models.CharField(max_length=120,default='')          #商户和支付宝交易的凭证
    status = models.CharField(max_length=20,default='待付款')       #订单状态：待付款、待发货、待收货、待评价、完成
    payway = models.CharField(max_length=20,default='alipay')       #支付方式：alipay、wepay、union

#订单明细模型
class OrderItem(models.Model):
    goodsid = models.IntegerField()
    sizeid = models.IntegerField()
    colorid = models.IntegerField()
    count = models.PositiveIntegerField()                         #数量  正整数
    order = models.ForeignKey(Order,on_delete=models.CASCADE)     #订单和订单明细是一对多关系

    def goods(self):
        return Goods.objects.get(id=self.goodsid)

    def total_price(self):
        return int(self.count) * float(self.goods().price)